#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :st_download.py
# @Time      :2024/10/24 20:58
# @Author    :zhou_yi

import streamlit as st


# 创建并下载文件到本地
def create_download_file(btn_label, *, fn, options: dict, download_filename: str):
    col1, col2 = st.columns([2, 8])
    with col1:
        btn = st.button(btn_label)

    if btn:
        res = fn(**options)  # 在服务器上生成文件返回文件地址
        if res:
            # 从服务器上读取刚生成的文件
            with open(res, 'rb') as _f:
                _bin = _f.read()

            with col2:
                try:
                    st.download_button(label=f"点击下载<{download_filename}>",
                                       data=_bin,
                                       file_name=download_filename,
                                       )
                except:
                    st.error('下载文件发生错误....')
                    st.stop()

        else:
            st.error('生成文件发生错误....')
            st.stop()





if __name__ == '__main__':
    ...
